package shooter.handler;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import shooter.Shooter;
import shooter.models.Player;
import shooter.network.MovementMessage;
import shooter.network.crossClient;

/**
 * This handler receives every input from the keyboard of a 
 * specific player. If it is a client the handler would send 
 * MovementMessages to the server.
 * 
 * @author Michail Tausch
 */
public class InputHandler implements KeyListener {

	int pindex;
	Shooter shooter;
	crossClient crossClient;
	
	/**
	 * The constructor of this class. Creates a new InputHandler object.
	 * @param playerIndex
	 * @param shooter
	 * @param crossClient
	 */
	public InputHandler(int playerIndex, Shooter shooter, crossClient crossClient) {
		super();
		this.pindex = playerIndex;
		this.shooter = shooter;
		this.crossClient=crossClient;
	}
	
	/**
	 * If a key is pressed this method is called.
	 * It handles each input.
	 */
	@Override
	public void keyPressed(KeyEvent e) {
		synchronized (this.shooter.getLock()) {
			Player player = this.shooter.getPlayer(this.pindex);
			switch (e.getKeyCode()) {
			case KeyEvent.VK_W:
				player.setUp(true);
				if(crossClient!=null){
					MovementMessage mM = createMovementMessage();
					crossClient.sendMessage(mM);
					crossClient.sendMessage(mM);
				}
				break;
			case KeyEvent.VK_S:
				player.setDown(true);
				if(crossClient!=null){
					MovementMessage mM = createMovementMessage();
					crossClient.sendMessage(mM);
					crossClient.sendMessage(mM);
				}				
				break;
			case KeyEvent.VK_A:
				player.setLeft(true);
				if(crossClient!=null){
					MovementMessage mM = createMovementMessage();
					crossClient.sendMessage(mM);
					crossClient.sendMessage(mM);
				}				
				break;
			case KeyEvent.VK_D:
				player.setRight(true);
				if(crossClient!=null){
					MovementMessage mM = createMovementMessage();
					crossClient.sendMessage(mM);
					crossClient.sendMessage(mM);
				}				
				break;
			case KeyEvent.VK_Q:
				player.setStrafeleft(true);
				if(crossClient!=null){
					MovementMessage mM = createMovementMessage();
					crossClient.sendMessage(mM);
					crossClient.sendMessage(mM);
				}				
				break;
			case KeyEvent.VK_E:
				player.setStraferight(true);
				if(crossClient!=null){
					MovementMessage mM = createMovementMessage();
					crossClient.sendMessage(mM);
					crossClient.sendMessage(mM);
				}				
				break;
			case KeyEvent.VK_R:
				if (!shooter.isSM()) {
					player.setRearm(true);
					player.setFire(false);
					player.setShoot(false);
					if(crossClient!=null){
						MovementMessage mM = createMovementMessage();
						crossClient.sendMessage(mM);
						crossClient.sendMessage(mM);
					}
				}
				break;
			case KeyEvent.VK_SPACE:
				if (player.isRearm() == false) {
					player.setShoot(true);
					if(crossClient!=null){
						MovementMessage mM = createMovementMessage();
						crossClient.sendMessage(mM);
						crossClient.sendMessage(mM);
					}					
				}
				break;
			case KeyEvent.VK_0:
				if (!shooter.isSM()) {
					player.setWeaponMode(0);
					player.setFire(false);
					player.setRearm(false);
					if(crossClient!=null){
						MovementMessage mM = createMovementMessage();
						crossClient.sendMessage(mM);
						crossClient.sendMessage(mM);
					}
				}
				break;
			case KeyEvent.VK_1:
				if (!shooter.isSM()) {
					player.setWeaponMode(1);
					player.setFire(true);
					player.setRearm(false);
					player.reloadTime=0.0;
					if(crossClient!=null){
						MovementMessage mM = createMovementMessage();
						crossClient.sendMessage(mM);
						crossClient.sendMessage(mM);
					}
				}
				break;
			case KeyEvent.VK_2:
				if (!shooter.isSM()) {
					player.setWeaponMode(2);
					player.setFire(true);
					player.setRearm(false);
					player.reloadTime=0.0;
					if(crossClient!=null){
						MovementMessage mM = createMovementMessage();
						crossClient.sendMessage(mM);
						crossClient.sendMessage(mM);
					}
					break;
				}
			case KeyEvent.VK_3:
				if (!shooter.isSM()) {
					player.setWeaponMode(3);
					player.setFire(true);
					player.setRearm(false);
					player.reloadTime=0.0;
					if(crossClient!=null){
						MovementMessage mM = createMovementMessage();
						crossClient.sendMessage(mM);
						crossClient.sendMessage(mM);
					}
				}
				break;
			case KeyEvent.VK_4:
				if (!shooter.isSM()) {
					player.setWeaponMode(4);
					player.setFire(true);
					player.setRearm(false);
					player.reloadTime=0.0;
					if(crossClient!=null){
						MovementMessage mM = createMovementMessage();
						crossClient.sendMessage(mM);
						crossClient.sendMessage(mM);
					}
				}
				break;
			case KeyEvent.VK_TAB:
				shooter.setStats(true);
				break;
			}
		}
		e.consume();
	}
	
	/**
	 * If a key is released this method is called.
	 * It handles each release event.
	 */
	@Override
	public void keyReleased(KeyEvent e) {
		// System.out.println("Key released");
		synchronized (this.shooter.getLock()) {
			Player player = this.shooter.getPlayer(pindex);

			switch (e.getKeyCode()) {
			case KeyEvent.VK_W:
				player.setUp(false);
				if(crossClient!=null){
					MovementMessage mM = createMovementMessage();
					crossClient.sendMessage(mM);
					crossClient.sendMessage(mM);
				}				
				break;
			case KeyEvent.VK_S:
				player.setDown(false);
				if(crossClient!=null){
					MovementMessage mM = createMovementMessage();
					crossClient.sendMessage(mM);
					crossClient.sendMessage(mM);
				}				
				break;
			case KeyEvent.VK_A:
				player.setLeft(false);
				if(crossClient!=null){
					MovementMessage mM = createMovementMessage();
					crossClient.sendMessage(mM);
					crossClient.sendMessage(mM);
				}				
				break;
			case KeyEvent.VK_D:
				player.setRight(false);
				if(crossClient!=null){
					MovementMessage mM = createMovementMessage();
					crossClient.sendMessage(mM);
					crossClient.sendMessage(mM);
				}				
				break;
			case KeyEvent.VK_Q:
				player.setStrafeleft(false);
				if(crossClient!=null){
					MovementMessage mM = createMovementMessage();
					crossClient.sendMessage(mM);
					crossClient.sendMessage(mM);
				}				
				break;
			case KeyEvent.VK_E:
				player.setStraferight(false);
				if(crossClient!=null){
					MovementMessage mM = createMovementMessage();
					crossClient.sendMessage(mM);
					crossClient.sendMessage(mM);
				}				
				break;
			case KeyEvent.VK_SPACE:
				player.setShoot(false);
				if(crossClient!=null){
					MovementMessage mM = createMovementMessage();
					crossClient.sendMessage(mM);
					crossClient.sendMessage(mM);
				}				
				break;
			case KeyEvent.VK_TAB:
				shooter.setStats(false);
				shooter.repaintStats(shooter.getG());
				shooter.repaintStats(shooter.getG());
				break;
			}
		}
		e.consume();
	}
	
	/**
	 * Handles all keyTyped events.
	 */
	@Override
	public void keyTyped(KeyEvent e) {
		// System.out.println("Key typed");
	}
	
	/**
	 * Returns a MovementMessage which has all specific parameters.
	 * @return
	 */
	public MovementMessage createMovementMessage(){
		Player player = this.shooter.getPlayer(this.pindex);
		MovementMessage mM = new MovementMessage(this.pindex);
		
		mM.setDown(player.isDown());
		mM.setLeft(player.isLeft());
		mM.setRight(player.isRight());
		mM.setUp(player.isUp());
		mM.setStrafeLeft(player.isStrafeleft());
		mM.setStrafeRight(player.isStraferight());
		mM.setShoot(player.isShoot());
		mM.setWeaponMode(player.getWeaponMode());
		mM.setReload(player.isRearm());
		
		return mM;		
	}
}
